<?php
namespace app\index\controller;

use app\common\controller\BaseHome;
use app\common\model\Arctype;
use app\common\model\Archive;
use app\common\model\ZanLog;
use app\common\model\ArchiveReply;

class Detail extends BaseHome {
    public function _initialize(){
        parent::_initialize();
    }

    public function index($dirs, $id) {
        $arctypeModel = new Arctype();
		$ArchiveReply = new ArchiveReply();
        $arctype = $arctypeModel->where(['dirs'=>$dirs])->order('id DESC')->find();
        $arctype->arctypeMod;

        $archiveModel = new Archive();
        $archive = $archiveModel->where(['id'=>$id, 'status'=>1])->find();
        if (empty($archive)){
            //跳转404
        }
        $archive['addondata'] = $archive->{$arctype->arctypeMod->mod};   //拓展模式表数据
        unset($archive[$arctype->arctypeMod->mod]);

        if($arctype['pid'] == '0'){
            $parent = $arctype;
        }else{
            $arctypeModel = new Arctype();
            $parent = $arctypeModel->topArctypeData($arctype['pid']);
        }
		$typelist = $arctypeModel->where(['pid'=>$parent['id']])->order('id DESC')->select();
		$reply_list = $ArchiveReply->where(['aid'=>$id,'audit'=>1])->select();

		$treeClass = new \expand\Tree();
	    $reply_list = $treeClass->create($reply_list);

		$this->assign('reply_list', $reply_list);
        $this->assign('typelist', $typelist);   //当前文章栏目顶级栏目信息
        $this->assign('parent', $parent);   //当前栏目顶级栏目信息
        $this->assign('arctype', $arctype);   //当前文章栏目信息
        $this->assign('archive', $archive);   //当前文章信息
        return $this->fetch($arctype['temparticle']);   //栏目模板
    }

	public function arc_zan(){
		if(request()->isPost()){
			$zanlog = new ZanLog();
			$data = input('post.');
			if(empty($this->uid)){
				return ajaxReturn('亲！请登录点赞哦。','',2);
			}
			if( $data['type'] == 'reply'){
				$zanlogdata = $zanlog->where(['ar_id'=>$data['ar_id'],'uid'=>$this->uid])->find();
				if($zanlogdata){
					return ajaxReturn('您已经赞过啦！');
				}else{
					$setInc = db('ArchiveReply')->where('id', $data['ar_id'])->setInc('zan_num');//赞 +1
					if( !empty($setInc) ){
						$zanadd = $zanlog->save(['ar_id'=>$data['ar_id']]);//添加记录
						if($zanadd){
							return ajaxReturn('感谢您的点赞！','',1);
						}
					}
				}
			}else if( $data['type'] == 'archive'){
				$zanlogdata = $zanlog->where(['a_id'=>$data['id'],'uid'=>$this->uid])->find();
				if($zanlogdata){
					return ajaxReturn('您已经赞过啦！');
				}else{
					$setInc = db('Archive')->where('id', $data['id'])->setInc('zan_num');//赞 +1
					if( !empty($setInc) ){
						$zanadd = $zanlog->save(['a_id'=>$data['id']]);//添加记录
						if($zanadd){
							return ajaxReturn('感谢您的点赞！','',1);
						}
					}
				}
			}
		}

	}

	public function arc_reply(){	//回复
		if(request()->isPost()){
			$ArchiveReply = new ArchiveReply();
			$data = input('post.');
			if(empty($this->uid)){
				return ajaxReturn('亲！登录后才可以回复哦。','',2);
			}
			$add = $ArchiveReply->validate('ArchiveReply.add')->allowField(true)->save($data);
			if( $add ){
				$setInc = db('Archive')->where('id', $data['aid'])->setInc('reply_num');//回复 +1
				if($data['pid'] != 0){
					$setIncs = db('ArchiveReply')->where(['id'=>$data['pid'],'aid'=>$data['aid']])->setInc('reply_num');//父级回复 +1
				}
				if( !empty($setInc) ){
					return ajaxReturn('回复成功！','',1,$data);
				}
			}else{
				return ajaxReturn($ArchiveReply->getError());
			}
		}
	}

	public function addArticle(){//添加文章
		if(request()->isPost()){
			if(empty($this->uid)){
				return ajaxReturn('请登录发布','',2);
			}
			$data = input('post.');
			$data['status'] = confv('is_addarticle_audit','system');
			$archiveModel = new Archive();
			$result = $archiveModel->validate('Archive.add')->allowField(true)->save($data);
            $datas['aid'] = $archiveModel->getLastInsID();
			$datas['content'] = $data['content'];
            $mod = $data['mod'];
            $addonData = db($mod)->field('id', true)->strict(false)->insert($datas);   //新增关联表数据
            if ($result && $addonData){
            	if(confv('is_addarticle_audit','system') == 0){
            		$info = '发布成功，需要审核后才能显示。';
            	}else{
            		$info = '发布成功';
            	}
                return ajaxReturn($info, url('/category/post'));
            }else{
                return ajaxReturn($archiveModel->getError());
            }
		}else{
			$arctype = new Arctype();
			$arclist = $arctype->where(['status'=>1,'is_release'=>1])->order('sorts ASC,id ASC')->select();
			$this->assign('titles','添加');
			$this->assign('arclist',$arclist);
			return $this->fetch();
		}
	}




}
